<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" media="screen" href="../../global/css/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" media="screen" href="../../global/css/font-awesome/css/font-awesome.css">
    </head>
    <body>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <form class="form-horizontal" onsubmit="return false" id="form"  enctype="multipart/form-data">
                <fieldset>
                <input type="hidden" name="goodsId" id="goodsId" />
                <div class="form-group">
                <label class="col-md-2 control-label">商品规格</label>
                <div class="col-md-10" id="col-md-10">
                    <input type="text"  placeholder="规格明细" name="specDetail" /><input type="text"  placeholder="库存数量" name="specNum" /><input type="text"  placeholder="排序" name="specSort" /><input type="button" class="addSpec" value="新增规格" />
                </div>
            </div>

                    <div class="form-actions">
                        <div class="row" align="center">
                            <div class="col-md-12">
                                <button class="btn btn-primary" onclick="location.href='goodsList.html'">返回</button>
                                <button class="btn btn-primary" type="submit" onclick="add()">
                                    <i class="fa fa-save"></i>
                                    保存
                                </button>
                            </div>
                        </div>
                    </div>

                </fieldset>
            </form>
        </div>

        <script type="text/javascript" src="../../global/js/libs/jquery-2.1.1.min.js"></script>
        <script type="text/javascript" src="../../global/js/jq.js"></script>
        <script type="text/javascript"
                src="../../global/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
        <script type="text/javascript" src="../../global/js/common.js"></script>
        <script type="text/javascript" src="../../global/js/ajaxfileupload.js"></script>
        <script type="text/javascript" src="../../global/js/my/ztree-menu.js"></script>
        <script type="text/javascript" src="../../global/layui/layui.js"></script>
        <script type="text/javascript">
        layui.use('layer', function () {
            var layer = layui.layer;
        });
        var goodsId = getUrlParam("goodsId");
        $("#goodsId").val(goodsId);
        if (goodsId != "") {
            $.ajax({
                type: 'get',
                url: '/clyjGoods/' + goodsId,
                async: false,
                success: function (data) {
                	console.info(data);
                	 if(!data.data2){
                     	alert("商品暂无规格参数，请先添加再上传！");
                     	location.href="goodsList.html";
                     }
                    var list = data.data2.specDetail;
                    console.info(list);
                    if(list==null || list.length==0){
                    	alert("商品暂无规格参数，请先添加再上传！");
                    	location.href="goodsList.html";
                    }
                    $("#col-md-10").empty();
                    for(var i=0;i<list.length;i++){
                    	$("#col-md-10").append('<br/><div id="div'+i+'"><input type="hidden" value="'+data.data2.specId[i]+'" name="specId" /><input type="text" value="'+list[i]+'"  placeholder="规格明细" name="specDetail" /><input value="'+data.data2.specNum[i]+'" type="text"  placeholder="库存数量" name="specNum" /><input type="text" value="'+data.data2.specSort[i]+'"  placeholder="排序" name="specSort" /><input name="file'+i+'" type="file" multiple  class="addSpec" value="上传轮播图" /></div>');
                    	var picList = data.data2.specPic[i];
                    	if(picList!=null&&picList.length>0){
                    		for(var j=0;j<picList.length;j++){
                    			$("#div"+i).append("<img src='"+picList[j]+"' style='width:100px;height:60px' />")
                    		}
                    	}
                    }
                }
            });
        }
        
        function add(){
        	$("form").ajaxSubmit({
   			 type: 'post',
               url: '/clyjGoods/saveSpecPic',
               traditional: true,
               contentType: "application/json; charset=utf-8",
               success: function (data) {
                      layer.msg("添加成功", {shift: -1, time: 1000}, function () {
                          location.href = "goodsList.html";
                      });
                  }
   		})
        }

        </script>
    </body>
</html>